package com.woniu.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.client.AccountClient;
import com.woniu.client.ProductClient;
import com.woniu.commons.model.StateData;
import com.woniu.dao.mapper.YLeagueClassDetailMapper;
import com.woniu.model.model.YLeagueClass;
import com.woniu.model.dto.LeagueClassDetailDto;
import com.woniu.model.form.AddLeagueClassDetailForm;
import com.woniu.model.model.YLeagueClassDetail;
import com.woniu.model.model.YUserLeagueClassMargin;
import com.woniu.service.YLeagueClassDetailService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 胡歌
 * @since 2024年05月28日
 */
@Service
public class YLeagueClassDetailServiceImpl extends ServiceImpl<YLeagueClassDetailMapper, YLeagueClassDetail> implements YLeagueClassDetailService {

    @Resource
    private ProductClient productClient;
    @Resource
    private AccountClient accountClient;

    /**
     * 新增团课详情（跟踪）
     * @param form
     */
    @Override
    public void addLeagueClassDetail(AddLeagueClassDetailForm form) {

        List<YUserLeagueClassMargin> margins = form.getMargins();

        for (YUserLeagueClassMargin margin:margins
             ) {
            Integer userLeagueClassMarginId = margin.getId();
            YLeagueClassDetail detail = new YLeagueClassDetail();
            detail.setStartTime(form.getStartTime());
            detail.setEndTime(form.getEndTime());
            detail.setCoachId(form.getCoachId());
            detail.setUserId(margin.getUserId());
            detail.setUserLeagueClassMarginId(userLeagueClassMarginId);
            detail.setLeagueClassState(StateData.LEAGUE_CLASS_STATE_FULL);//未完成
            detail.setLeagueClassId(form.getLeagueClassId());
            detail.setLeagueClassWorkforceId(form.getLeagueClassWorkforceId());
            baseMapper.insert(detail);
        }

    }

    /**
     * 完成当堂课的详情
     * @param leagueClassWorkforceId
     */
    @Override
    public void finishLeagueClassDetail(Integer leagueClassWorkforceId) {
        List<YLeagueClassDetail> leagueClassDetails = baseMapper.selectList(new QueryWrapper<YLeagueClassDetail>().eq("league_class_workforce_id", leagueClassWorkforceId));
        for (YLeagueClassDetail l: leagueClassDetails
             ) {
            l.setLeagueClassState(StateData.LEAGUE_CLASS_STATE_START);//完成
            baseMapper.updateById(l);
        }
    }

    @Override
    public void cancelLeagueClassDetail(Integer leagueClassWorkforceId) {
        List<YLeagueClassDetail> leagueClassDetails = baseMapper.selectList(new QueryWrapper<YLeagueClassDetail>().eq("league_class_workforce_id", leagueClassWorkforceId));
        for (YLeagueClassDetail l: leagueClassDetails
        ) {
            l.setLeagueClassState(StateData.LEAGUE_CLASS_STATE_DISABLED);//取消
            baseMapper.updateById(l);
        }
    }

    @Override
    public List<LeagueClassDetailDto> loadLeagueClassDetailByUser(Integer userId, Integer classState,String leagueClassName) {
        QueryWrapper<YLeagueClassDetail> wrapper = new QueryWrapper<>();

        if (classState != null){
            wrapper.eq("league_class_state",classState);
        }
        if (leagueClassName != null){

            List<com.woniu.model.YLeagueClass> list = productClient.loadLeagueClassByName(leagueClassName).getData();

            Set<Integer> collect = list.stream().map(o -> o.getLeagueClassId()).collect(Collectors.toSet());
            wrapper.in("league_class_id",collect);
        }
        wrapper.eq("user_id",userId);

        List<YLeagueClassDetail> details = baseMapper.selectList(wrapper);

        ArrayList<LeagueClassDetailDto> dtos = new ArrayList<>();
        for (YLeagueClassDetail d:details
             ) {
            LeagueClassDetailDto dto = new LeagueClassDetailDto();
            dto.setDetail(d);
            dto.setLeagueClass(BeanUtil.toBean( productClient.ByIdLeagueClass(d.getLeagueClassId()).getData(),YLeagueClass.class));
            dto.setCoach(accountClient.loadCoachById(d.getCoachId()).getData());

            dtos.add(dto);
        }


        return dtos;
    }
}
